import 'dart:convert';
import 'package:flutter_my_web/constants/Global.dart';
import 'package:flutter_my_web/pages/about/about_page.dart';
import 'package:flutter_my_web/pages/files/file_pages.dart';
import 'package:flutter_my_web/pages/home/edit_get_home_data_dialog.dart';
import 'package:flutter_my_web/pages/setting/setting_page.dart';
import 'package:flutter_my_web/utils/Utils.dart';
import 'package:flutter_weather_bg_null_safety/bg/weather_bg.dart'
    deferred as weather_bg;
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_my_web/dialog/login_dialog.dart';
import 'package:flutter_my_web/logic/HomeGetLogic.dart';
import 'package:flutter_my_web/pages/tools/ToolsMainPage.dart';
import 'package:flutter_lib/utils/LogUtils.dart';
import 'package:flutter_lib/utils/StringUtils.dart';
import 'package:flutter_my_web/widget/BlueMaterialButton.dart';
import 'package:flutter_my_web/widget/ImageWidget.dart';
import 'package:flutter_my_web/widget/TextWidget.dart';
import 'package:flutter_lib/utils/DpUtils.dart';
import 'package:get/get.dart' as Getx;
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_navigation/src/snackbar/snackbar.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:responsive_framework/responsive_framework.dart';

class HomeTopBarWidget extends StatefulWidget {
  var mHomeGetLogic = Getx.Get.find<HomeGetLogic>().obs;

  @override
  _HomeTopBarWidgetState createState() => _HomeTopBarWidgetState();
}

class _HomeTopBarWidgetState extends State<HomeTopBarWidget> {
  @override
  Widget build(BuildContext context) {
    return Getx.Obx(() => Material(
          color: Color.fromARGB(
              widget.mHomeGetLogic.value.homeSingleViewOffset.value < 20
                  ? 0
                  : 200,
              66,
              185,
              131), //42B983cc
          child: Container(
            padding: EdgeInsets.all(60.0.wDp),
            height: 60.0.hDp,
            // color: Color(0xcc42b983),
            child: Center(
              child: Row(
                children: [
                  ResponsiveVisibility(
                    visible: false,
                    child: InkWell(
                      child: PopupMenuButton(
                        elevation: 10,
                        color: Color(0x33000000),
                        enableFeedback: true,
                        child: Icon(
                          Icons.menu,
                          color: Colors.white,
                          size: 30.0.wDp,
                        ),
                        itemBuilder: (BuildContext _) {
                          return [
                            PopupMenuItem(
                              value: 0,
                              child: TopCenterIconWidget(
                                _TopCenterIconBean("工具", Icons.home),
                              ),
                            ),
                            PopupMenuItem(
                              value: 1,
                              child: TopCenterIconWidget(
                                _TopCenterIconBean(
                                    "标签", Icons.assistant_photo_sharp),
                              ),
                            ),
                            PopupMenuItem(
                              value: 2,
                              child: TopCenterIconWidget(
                                _TopCenterIconBean("图库", Icons.image),
                              ),
                            ),
                            PopupMenuItem(
                              value: 3,
                              child: TopCenterIconWidget(
                                _TopCenterIconBean("文件夹", Icons.image),
                              ),
                            ),
                            PopupMenuItem(
                              value: 4,
                              child: TopCenterIconWidget(
                                _TopCenterIconBean(
                                    "关于", Icons.app_blocking_outlined),
                              ),
                            ),
                          ];
                        },
                        onSelected: (value) {
                          switch (value) {
                            case 0:
                              {
                                Get.to(ToolsMainPage());
                                break;
                              }
                            case 1:
                              {
                                break;
                              }
                            case 2:
                              {
                                if (!Global.isRoot) {
                                  showSnackBar("管理员才能访问");
                                  return;
                                }
                                break;
                              }
                            case 3:
                              {
                                if (!Global.isRoot) {
                                  showSnackBar("管理员才能访问");
                                  return;
                                }
                                Get.to(FilePage());
                                break;
                              }
                            case 4:
                              Get.to(AboutPage());
                              break;
                          }
                        },
                      ),
                    ),
                  ),
                  _UserInfoWidget(),
                  Spacer(),
                  ResponsiveVisibility(
                    visible: false,
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        TopCenterIconWidget(
                          _TopCenterIconBean("工具", Icons.home, click: () {
                            Get.to(ToolsMainPage());
                          }),
                        ),
                        TopCenterIconWidget(_TopCenterIconBean(
                            "标签", Icons.assistant_photo_sharp)),
                        TopCenterIconWidget(
                            _TopCenterIconBean("图库", Icons.image, click: () {
                          if (!Global.isRoot) {
                            showSnackBar("管理员才能访问");
                            return;
                          }
                        })),
                        TopCenterIconWidget(
                            _TopCenterIconBean("文件夹", Icons.image, click: () {
                          if (!Global.isRoot) {
                            showSnackBar("管理员才能访问");
                            return;
                          }

                          Get.to(FilePage());
                        })),
                        // _TopCenterIconWidget(
                        //     _TopCenterIconBean("分类", Icons.fence)),
                        TopCenterIconWidget(_TopCenterIconBean(
                            "关于", Icons.app_blocking_outlined, click: () {
                          Get.to(AboutPage());
                        })),
                        Container(
                          width: 200.0.wDp,
                          height: 40.0.hDp,
                          margin: EdgeInsets.only(left: 10.0.wDp),
                          child: TextField(
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 12.sp,
                            ),
                            textAlign: TextAlign.center, //文本对齐方式
                            autofocus: false, //是否自动获取焦点
                            decoration: InputDecoration(
                                hoverColor: Colors.white,
                                border: OutlineInputBorder(),
                                hintText: '输入查找的内容',
                                // labelText: '请输入',
                                prefixIcon: Icon(Icons.search),
                                labelStyle: TextStyle(
                                  color: Colors.white,
                                  fontSize: 10.sp,
                                ),
                                hintStyle: TextStyle(
                                  color: Colors.white,
                                  fontSize: 10.sp,
                                )),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    width: 20.0.wDp,
                  ),
                  // _WeatherWidget(),
                ],
              ),
            ),
          ),
        ));
  }
}

class _UserInfoWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if(!Global.isRoot){
          showLoginDialog(context);
        }else{
          Get.to(SettingPage());
        }

      },
      child: Row(
        children: [
          Container(
            height: 40.0.hDp,
            width: 40.0.hDp,
            child: ImageLoadUtils(
              url:
                  "https://img11.360buyimg.com/ddimg/jfs/t1/173445/21/15206/1609537/60caebaaE18d21c8d/757aea972636a1b8.png",
              radius: 90,
            ),
          ),
          Container(
            width: 10.0.wDp,
          ),
          SimpleText(
            "NESTS Blog",
            fontSize: 25.sp,
            color: Colors.white,
          ),
        ],
      ),
    );
  }
}

class _TopCenterIconBean {
  String title = "";
  IconData icon = Icons.add;
  Function? click;

  _TopCenterIconBean(this.title, this.icon, {this.click});
}

class TopCenterIconWidget extends StatefulWidget {
  _TopCenterIconBean __topCenterIconBean;

  TopCenterIconWidget(this.__topCenterIconBean);

  @override
  _TopCenterIconWidgetState createState() => _TopCenterIconWidgetState();
}

class _TopCenterIconWidgetState extends State<TopCenterIconWidget> {
  var isSelect = false;

  @override
  Widget build(BuildContext context) {
    var retWidget = Center(
      child: Row(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 10.0.wDp,
            ),
            Icon(
              widget.__topCenterIconBean.icon,
              color: Colors.white,
              size: 12.0.hDp,
            ),
            SimpleText(
              widget.__topCenterIconBean.title,
              fontSize: 16.sp,
              color: Colors.white,
            ),
            Container(
              width: 10.0.wDp,
            ),
          ]),
    );

    return SizedBox(
      height: 30.0.hDp,
      child: widget.__topCenterIconBean.click == null
          ? MouseRegion(
              // onEnter: (event) {
              //   setState(() {
              //     isSelect = true;
              //   });
              // },
              onHover: (event) {
                setState(() {
                  isSelect = true;
                });
              },
              onExit: (event) {
                setState(() {
                  isSelect = false;
                });
              },
              child: Container(
                  foregroundDecoration: BoxDecoration(
                    color: isSelect ? Color(0x77E3F2FD) : Colors.transparent,
                  ),
                  child: retWidget))
          : InkWell(
              onTap: () {
                widget.__topCenterIconBean.click?.call();
              },
              hoverColor: Color.fromARGB(66, 66, 185, 131),
              child: retWidget,
            ),
    );
  }
}

// class _WeatherWidget extends StatefulWidget {
//   @override
//   State<_WeatherWidget> createState() => _WeatherWidgetState();
// }
//
// class _WeatherWidgetState extends State<_WeatherWidget> {
//   var bean = Get.find<HomeGetLogic>().mTianqiapiWeatherReponseEntity;
//
//   @override
//   Widget build(BuildContext context) {
//     //先只获取厦门的
//     if (!bean.value.haveData) {
//       Dio()
//           .get(
//               'https://tianqiapi.com/free/day?appid=67841542&appsecret=1odGZYtr&city=%E5%8E%A6%E9%97%A8')
//           .then((value) {
//         LogUtils.e("获取天气：${value.toString()}");
//         bean.value = TianqiapiWeatherReponseEntity()
//             .fromJson(jsonDecode(value.toString()));
//         if (bean.value == null ||
//             bean.value.city == null ||
//             bean.value.city!.isEmpty) {
//           String da = "{\n" +
//               "    \"cityid\": \"101230201\",\n" +
//               "    \"city\": \"厦门\",\n" +
//               "    \"update_time\": \"19:27\",\n" +
//               "    \"wea\": \"阵雨\",\n" +
//               "    \"wea_img\": \"lei\",\n" +
//               "    \"tem\": \"27\",\n" +
//               "    \"tem_day\": \"32\",\n" +
//               "    \"tem_night\": \"26\",\n" +
//               "    \"win\": \"东风\",\n" +
//               "    \"win_speed\": \"2级\",\n" +
//               "    \"win_meter\": \"10km/h\",\n" +
//               "    \"air\": \"25\"\n" +
//               "}";
//           bean.value = TianqiapiWeatherReponseEntity().fromJson(jsonDecode(da));
//         }
//         bean.value.haveData = true;
//       });
//     }
//
//     return Getx.Obx(() => InkWell(
//           onHover: (value) async {
//             await weather_bg.loadLibrary();
//             if (value) {
//               if (bean.value.haveData) {
//                 Get.put(bean.value);
//                 Navigator.push(context, WeatherDialogPopRoute());
//               }
//             }
//           },
//           onTap: () async {
//             await weather_bg.loadLibrary();
//             if (bean.value.haveData) {
//               Get.put(bean.value);
//               Navigator.push(context, WeatherDialogPopRoute());
//             }
//           },
//           child: Row(
//             children: [
//               SimpleText(
//                 "${StringUtils.stringPrint(bean.value.tem)}°",
//                 color: Colors.white,
//               ),
//               Container(
//                 width: 5.0.wDp,
//               ),
//               SimpleText(
//                 "${StringUtils.stringPrint(bean.value.city)}",
//                 color: Colors.white,
//               ),
//               Container(
//                 width: 5.0.wDp,
//               ),
//               SimpleText(
//                 "${StringUtils.stringPrint(bean.value.wea)}",
//                 color: Colors.white,
//               ),
//               Container(
//                 width: 5.0.wDp,
//               ),
//               // BlueMaterialButton("更换天气", () {})
//             ],
//           ),
//         ));
//   }
// }
